package edu.njust.service;

import edu.njust.pojo.*;
import edu.njust.dao.*;
import edu.njust.dao.ComputerMapper;
import edu.njust.dao.ReservationMapper;
import edu.njust.dao.UserMapper;
import edu.njust.pojo.*;
import edu.njust.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import sun.font.TrueTypeFont;


import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;
import java.util.*;

public class CourseServiceImpl implements CourseService{

    private DateUtils dateUtils;

    private UserMapper userMapper;

    public UserMapper getUserMapper(){
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    private ComputerMapper computerMapper;

    public ComputerMapper getComputerMapper(){
        return computerMapper;
    }

    public void setComputerMapper(ComputerMapper computerMapper) {
        this.computerMapper = computerMapper;
    }

    private ReservationMapper reservationMapper;

    public ReservationMapper getReservationMapper(){
        return reservationMapper;
    }

    public void setReservationMapper(ReservationMapper reservationMapper) {
        this.reservationMapper = reservationMapper;
    }

    private RecordMapper recordMapper;

    public RecordMapper getRecordMapper(){
        return recordMapper;
    }

    public void setRecordMapper(RecordMapper recordMapper) {
        this.recordMapper = recordMapper;
    }

    //√
    //通过学生学号获取课程列表
//    @Override
//    public List<Reservation> getReservationListById(String id) {
//        String klassId = userMapper.getKlassIdById(id);
//        List<Course> courseList= userMapper.getCourseListByKlassId(klassId);
//        List<KlassReservation> klassReservationList = reservationMapper.getKlassReservationListByCourseList(courseList);
//        List<ExamReservation> examReservationList = reservationMapper.getExamReservationListByCourseList(courseList);
//        return this.mergeReservation(klassReservationList,examReservationList,null);
//    }
//
//    //√
//    //通过学生学号以及日期获取当天课程列表
//    @Override
//    public List<Reservation> getReservationListByIdAndDate(String id, Date date) {
//        String klassId = userMapper.getKlassIdById(id);
//        List<Course> courseList= userMapper.getCourseListByKlassId(klassId);
//        List<KlassReservation> klassReservationList = reservationMapper.getKlassReservationListByDateAndCourseList(date, courseList);
//        List<ExamReservation> examReservationList = reservationMapper.getExamReservationListByDateAndCourseList(date, courseList);
//        return this.mergeReservation(klassReservationList,examReservationList,null);
//    }
//
//    //√
//    //通过学生学号、日期以及第几大节课获取该时间段课表
//    @Override
//    public List<Reservation> getReservationListByIdAndDateAndTimeNum(String id, Date date, Integer time_num) {
//        String klassId = userMapper.getKlassIdById(id);
//        List<Course> courseList= userMapper.getCourseListByKlassId(klassId);
//        List<KlassReservation> klassReservationList = reservationMapper.getKlassReservationListByDateCourseListAndTimeNum(date, courseList, time_num);
//        List<ExamReservation> examReservationList = reservationMapper.getExamReservationListByDateCourseListAndTimeNum(date, courseList,time_num);
//        return this.mergeReservation(klassReservationList,examReservationList,null);
//    }

    //1 学生查询需要进行（可选）的实验课
    @Override
    public List<ReservationItem> searchExistingKlassReservationForStudent(String student_id) {
        String klassId = userMapper.getKlassIdById(student_id);
        System.out.println(klassId);
        List<ReservationItem> itemList = reservationMapper.getItemListByKlassIdAndType(klassId,1);
        System.out.println(itemList);
        List<Record> recordList = recordMapper.getRecordListByStudentIDAndType(student_id,1);
        Iterator<ReservationItem> it;
        for(Record record :recordList){
            it = itemList.iterator();
            while(it.hasNext()){
                ReservationItem reservationItem = it.next();
                if(reservationItem.getReservation_name().equals(record.getReservation_name())){
                    it.remove();
                }
            }
        }
        return itemList;
    }

    //2 学生查询需要进行的考试
    @Override
    public List<ReservationItem> searchOwnExamReservationForStudent(String student_id) {
        List<Record> recordList = recordMapper.getRecordListByStudentIDAndType(student_id,2);
        List<ReservationItem> reservationItemList = new ArrayList<>();
        for(Record record :recordList){
            ReservationItem reservationItem = reservationMapper.getReservationItemByItemId(record.getReservationItem_id());
            //TODO 返回座位号放在“klass_id”中
            reservationItem.setKlass_id(record.getSeat_num().toString());
            reservationItemList.add(reservationItem);
        }
        return reservationItemList;
    }

    //3 学生查询已选的实验课
    @Override
    public List<ReservationItem> getSelectedKlassReservation(String student_id) {
        List<Record> recordList = recordMapper.getRecordListByStudentIDAndType(student_id,1);
        List<ReservationItem> reservationItemList = new ArrayList<>();
        for(Record record :recordList){
            ReservationItem reservationItem = reservationMapper.getReservationItemByItemId(record.getReservationItem_id());
            reservationItemList.add(reservationItem);
        }
        return reservationItemList;
    }

    //4 学生查询已有的比赛
    @Override
    public List<ReservationItem> getExistingCompetitionReservation(String student_id) {
        List<Record> recordList = recordMapper.getRecordListByStudentIDAndType(student_id,4);
        List<ReservationItem> reservationItemList = new ArrayList<>();
        for(Record record :recordList){
            ReservationItem reservationItem = reservationMapper.getReservationItemByItemId(record.getReservationItem_id());
            //TODO 返回座位号放在“klass_id”中
            reservationItem.setKlass_id(record.getSeat_num().toString());
            reservationItemList.add(reservationItem);
        }
        return reservationItemList;
    }

    //5 学生选择实验课
    @Override
    public Integer selectKlassReservationForStudent(Long reservationItem_id,String student_id) {
        ReservationItem reservationItem = reservationMapper.getReservationItemByItemId(reservationItem_id);
        //ToDo
        if(reservationItem.getSelected()<reservationItem.getIs_usage()){
            reservationItem.selectReservationItem();
            reservationMapper.updateReservationItem(reservationItem);
            Record record = new Record(student_id,reservationItem_id,reservationItem.getReservation_name(),1,1);
            Long recordId = recordMapper.getLastRecordId() + 1;
            record.setId(recordId);
            recordMapper.addRecord(record);
        }
        else{
            return 0;
        }
        return 1;
    }

    //6 学生退课
    @Override
    public Integer quitKlassReservationForStudent(Long reservationItem_id, String student_id) {
        ReservationItem reservationItem = reservationMapper.getReservationItemByItemId(reservationItem_id);
        System.out.println(reservationItem);
        Record record = recordMapper.getRecordByItemId(reservationItem_id);
        System.out.println(record);
        if(reservationItem.getSelected()>0){
            reservationItem.quitReservationItem();
            System.out.println(reservationItem);
            reservationMapper.updateReservationItem(reservationItem);
            System.out.println(reservationItem);
            System.out.println(record);
            record.cancel();
            System.out.println(record);
            recordMapper.quitRecord(record);
        }
        else{
            return 0;
        }
        return 1;
    }

    //√
    //7 通过老师Id查询自己的预约
    @Override
    public List<KlassReservation> getReservationListByTeacherId(String teacher_id) {
        return reservationMapper.getKlassReservationListByTeacherId(teacher_id);
    }

    //8 通过预约id取消预约
    @Override
    public Integer quitReservation(Long reservation_id,Integer reservation_type) {
        System.out.println(reservation_id);
        if(reservation_type == 1){
            reservationMapper.quitKlassReservation(reservation_id);
        }
        else if(reservation_type == 2){
            reservationMapper.quitExamReservation(reservation_id);
        }
        else if(reservation_type == 3){
            reservationMapper.quitLabReservation(reservation_id);
        }
        else if(reservation_type == 4){
            reservationMapper.quitCompetitionReservation(reservation_id);
        }
        return quitReservationItem(reservation_id);
    }

    //9 item取消
    @Override
    public Integer quitReservationItem(Long reservation_id) {
        System.out.println(reservation_id);
        List<Long> reservationIdList = new ArrayList<>();
        reservationIdList.add(reservation_id);
        List<ReservationItem> itemList = reservationMapper.getItemListByReservationId(reservationIdList);

        System.out.println(itemList);
        List<Long> idList = new ArrayList<>();
        for (ReservationItem reservationItem : itemList)
            idList.add(reservationItem.getId());
        if(reservationMapper.quitReservationItem(idList)==false)
            return 0;

        System.out.println(idList);
        List<Record> recordList = recordMapper.getRecordListByItemList(itemList);
        List<Long> recordIdList = new ArrayList<>();
        for(Record record :recordList){
            recordIdList.add(record.getId());
        }
        return recordMapper.quitRecordList(recordIdList);
    }

    //√
    //10 根据选课类型的需求返回可选的机房列表
    @Override
    public List<ComputerRoom> getComputerRoomListByType(Date date, Integer time_num, Integer reservation_type) {
        List<ComputerRoom> computerRoomList = getComputerRoomList(date, time_num);
        if(reservation_type == 2){
            Iterator<ComputerRoom> it = computerRoomList.iterator();
            while (it.hasNext()) {
                ComputerRoom computerRoom = it.next();
                if (!computerRoom.getNum().equals(computerRoom.getRemain_num())) {
                    it.remove();
                }
            }
        }

        return computerRoomList;
    }

    //√
    //11 根据时间和序号显示该时间段机房信息
    @Override
    public List<ComputerRoom> getComputerRoomList(Date date, Integer time_num) {
        List<ComputerRoom> computerRoomList = initComputerRoomList(computerMapper.getAllComputerRoom());
        List<Reservation> reservationList = getReservationListByDateAndTimeNum(date, time_num);
        if(reservationList.isEmpty()){
            return computerRoomList;
        }
        List<ReservationItem> reservationItemList = reservationMapper.getReservationItemList(reservationList);
        Iterator<ComputerRoom> it;
        for(ReservationItem reservationItem : reservationItemList){
            it = computerRoomList.iterator();
            while (it.hasNext()) {
                ComputerRoom computerRoom = it.next();
                if (reservationItem.getComputer_room_id().equals(computerRoom.getId())) {
                    if (reservationItem.getShared() == 0) {
                        it.remove();
                    } else {
                        computerRoom.schedule(reservationItem.getIs_usage());
                    }
                }
            }
        }
        return computerRoomList;


    }




    //√
    //12 通过老师id和课程名显示课程班级信息
    @Override
    public List<Klass> getKlassList(String teacher_id, String course_name) {
        List<String> klassIdList = userMapper.getKlassIdByTeacherIdAndCourseName(teacher_id, course_name);
        List<Klass> klassList = new ArrayList<>();
        for (String id : klassIdList)
            klassList.add(userMapper.getKlassByKlassId(id));
        return klassList;
    }

    //√
    //13 开放性实验排课
    @Override
    public void addLabReservation(String teacher_id, String course_name, Date begin_time, Date end_time,Integer plan_num){
        Long reservationId= reservationMapper.getLastReservationId()+1;
        LabReservation labReservation = new LabReservation(teacher_id, course_name, 2, begin_time, end_time, plan_num, plan_num);
        labReservation.setId(reservationId);
        reservationMapper.addLabReservation(labReservation);
        ReservationItem reservationItem = new ReservationItem(labReservation.getId(),"0",course_name,"0",0,0,3);
        Long reservationItemId= reservationMapper.getLastReservationItemId()+1;
        reservationItem.setId(reservationItemId);
        reservationMapper.addReservationItem(reservationItem);
    }

    //14 通过比赛时间获取time_num的列表
    @Override
    public List<Integer> getConflictedTimeNumByTime(Time begin_time, Time end_time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String begin = sdf.format(begin_time);
        String end = sdf.format(end_time);
        List<Integer> time_num_list = new ArrayList<>();
        String[] stdTimeTable = {"08:00","10:25","10:40","12:15","14:00","15:35","15:50","18:15","19:00","21:25"};
        Integer firstOccupiedNum = 0;
        Integer lastOccupiedNum = 0;
        if(begin.compareTo(stdTimeTable[1])<0){
            firstOccupiedNum = 1;
        }
        else if(begin.compareTo(stdTimeTable[3])<0){
            firstOccupiedNum = 2;
        }
        else if(begin.compareTo(stdTimeTable[5])<0){
            firstOccupiedNum = 3;
        }
        else if(begin.compareTo(stdTimeTable[7])<0){
            firstOccupiedNum = 4;
        }
        else if(begin.compareTo(stdTimeTable[9])<0){
            firstOccupiedNum = 5;
        }

        if(end.compareTo(stdTimeTable[8])>0){
            lastOccupiedNum = 5;
        }
        else if(end.compareTo(stdTimeTable[6])>0){
            lastOccupiedNum = 4;
        }
        else if(end.compareTo(stdTimeTable[4])>0){
            lastOccupiedNum = 3;
        }
        else if(end.compareTo(stdTimeTable[2])>0){
            lastOccupiedNum = 2;
        }
        else if(end.compareTo(stdTimeTable[0])>0){
            lastOccupiedNum = 1;
        }
        if(firstOccupiedNum * lastOccupiedNum == 0)
            return null;
        for(int i = firstOccupiedNum;i<=lastOccupiedNum;++i){
            time_num_list.add(i);
        }
        return time_num_list;
    }

    @Override
    public List<CompetitionReservation> getConflictedCompetition(Date date, Time begin_time, Time end_time) {

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String begin = sdf.format(begin_time);
        String end = sdf.format(begin_time);
        List<CompetitionReservation> competitionList = reservationMapper.getCompetitionByDate(date);
        List<CompetitionReservation> competitionReservations = new ArrayList<>();
        for(CompetitionReservation competitionReservation :competitionList){
            if(begin.compareTo(sdf.format(competitionReservation.getEnd_time()))>0 || end.compareTo(sdf.format(competitionReservation.getBegin_time()))<0){ }
            else{
                competitionReservations.add(competitionReservation);
            }
        }
        return competitionReservations;

    }

    //15 根据安排的比赛时间返回会产生冲突的课或者考试
    @Override
    public List<Reservation> getConflictedReservation(Date date, Time begin_time, Time end_time,List<String> computer_room_id) {
        List<Integer> conflictedTimeNums = getConflictedTimeNumByTime(begin_time, end_time);
        List<Reservation> reservationList = new ArrayList<>();
        for(Integer conflictedTimeNum :conflictedTimeNums){
            List<KlassReservation> klassReservations = reservationMapper.getKlassReservationListByDateAndTime(date,conflictedTimeNum);
            List<ExamReservation> examReservations = reservationMapper.getExamReservationListByDateAndTime(date ,conflictedTimeNum);
            reservationList.addAll(mergeReservation(klassReservations,examReservations,null,null));
        }
        System.out.println(reservationList);
        //TODO
//        List<CompetitionReservation> conflictedCompetition = getConflictedCompetition(date, begin_time, end_time);
//        for(CompetitionReservation competitionReservation: conflictedCompetition){
//            reservationList.add(competitionReservation.toReservation());
//        }
        if(reservationList.isEmpty()){
            return new ArrayList<>();
        }
        List<ReservationItem> reservationItemList = reservationMapper.getReservationItemList(reservationList);
        List<ReservationItem> conflictedReservationItemList = new ArrayList<>();
        System.out.println(reservationItemList);
        System.out.println(computer_room_id);
        Iterator<ReservationItem> it;
        for(String room_id :computer_room_id){
            it = reservationItemList.iterator();
            while(it.hasNext()){
                ReservationItem reservationItem = it.next();
                if(reservationItem.getComputer_room_id().equals(room_id)){
                    conflictedReservationItemList.add(reservationItem);
                }
            }
        }
        for(ReservationItem reservationItem :conflictedReservationItemList){
            System.out.println(getCourseInfo(reservationItem));
        }
        return getReservationListByItemList(conflictedReservationItemList);
    }

    @Override
    public List<Reservation> getReservationListByItemList(List<ReservationItem> reservationItemList){
        List<KlassReservation> klassReservations = new ArrayList<>();
        List<ExamReservation> examReservations = new ArrayList<>();
        List<LabReservation> labReservations = new ArrayList<>();
        List<CompetitionReservation> competitionReservations = new ArrayList<>();
        for(ReservationItem reservationItem :reservationItemList){
            if(reservationItem.getType() == 1) {
                klassReservations.add(reservationMapper.getKlassReservationById(reservationItem.getReservation_id()));
            }
            if(reservationItem.getType() == 2) {
                examReservations.add(reservationMapper.getExamReservationById(reservationItem.getReservation_id()));
            }
            if(reservationItem.getType() == 3) {
                labReservations.add(reservationMapper.getLabReservationById(reservationItem.getReservation_id()));
            }
            if(reservationItem.getType() == 4) {
                competitionReservations.add(reservationMapper.getCompetitionReservationById(reservationItem.getReservation_id()));
            }
        }
        List<Reservation> reservationList = new ArrayList<>(mergeReservation(klassReservations, examReservations, labReservations,competitionReservations));
        Set<Reservation> set = new HashSet<>(reservationList);
        reservationList = new ArrayList<>(set);
        return reservationList;
    }

    //16 添加比赛预约
    @Override
    public Integer addCompetitionReservation(String user_id, String Scheduling_name,
                   Date date, Time begin_time, Time end_time,  List<String> computer_room_id,List<String> student_id) {
        List<Reservation> conflictedReservation = getConflictedReservation(date, begin_time, end_time, computer_room_id);
        for(Reservation reservation :conflictedReservation){
            quitReservation(reservation.getId(),reservation.getType());
        }
        CompetitionReservation competitionReservation = new CompetitionReservation(user_id,Scheduling_name,4,date,begin_time,end_time);
        Long competitionReservationId = reservationMapper.getLastReservationId() + 1;
        competitionReservation.setId(competitionReservationId);
        reservationMapper.addCompetitionReservation(competitionReservation);
        return 0;
    }


    //ToDo 通过老师id获取该老师的课程
    @Override
    public List<Course> getCourseListForTeacher(String teacher_id) {
        return userMapper.getCourseListByTeacherId(teacher_id);
    }

    //ToDo 管理员获取所有课程信息
    @Override
    public List<Course> getAllCourseForAdmin() {
        return userMapper.getAllCourseList();
    }

    //ToDo 管理员通过关键字查询课程信息
    @Override
    public List<Course> getCourseListByInformation(String teacher_name,String course_name){
        List<String> teacherIdList = userMapper.getUserIdListLikeNameAndType(teacher_name,2);
        return userMapper.getCourseListByInformation(teacherIdList,course_name);
    }


    //1
    //17 确认排课
    @Override
    public void addKlassReservation(String teacher_id, String course_name, String scheduling_name,Date date, Integer time_num,
                               boolean is_shared, Integer reservation_type, List<String> computer_room_id, List<String> klass_idList) {
        List<Klass> klassList = userMapper.getKlassListByKlassIdList(klass_idList);
        List<ComputerRoom> spareComputerRoomList = getComputerRoomListByType(date, time_num, reservation_type);
        List<ComputerRoom> selectedComputerRoomList = new ArrayList<>();
        for(ComputerRoom computerRoom : spareComputerRoomList){
            for(String room_id : computer_room_id){
                if(room_id.equals(computerRoom.getId())){
                    selectedComputerRoomList.add(computerRoom);
                    break;
                }
            }
        }
        if(selectedComputerRoomList.isEmpty()){
            return ;
        }
        KlassReservation klassReservation = new KlassReservation(teacher_id, scheduling_name,
                1, course_name, date, time_num, is_shared);
        Long klassReservationId = reservationMapper.getLastReservationId() + 1;
        klassReservation.setId(klassReservationId);
        reservationMapper.addKlassReservation(klassReservation);
        addReservationItem(klassReservation.getId(),klassReservation.getName(),selectedComputerRoomList,klassList,null,is_shared,reservation_type);
    }

    //ToDo 需要准确的course_name安排考试
    //18 确认安排考试
    @Override
    public void addExamReservation(String teacher_id, String course_name, String scheduling_name,Date date, Integer time_num,
                                Integer reservation_type, List<String> computer_room_id) {
        List<String> course_idList = userMapper.getCourseIdListByCourseName(course_name);
        List<String> klassIdList = new ArrayList<>();
        for (String course_id : course_idList)
            klassIdList.add(userMapper.getKlassIdByCourseId(course_id));
        List<Klass> klassList = userMapper.getKlassListByKlassIdList(klassIdList);
        List<ComputerRoom> spareComputerRoomList = getComputerRoomListByType(date, time_num, reservation_type);
        List<ComputerRoom> selectedComputerRoomList = new ArrayList<>();
        for(ComputerRoom computerRoom : spareComputerRoomList){
            for(String room_id : computer_room_id){
                if(room_id.equals(computerRoom.getId())){
                    selectedComputerRoomList.add(computerRoom);
                    break;
                }
            }
        }
        if(selectedComputerRoomList.isEmpty()){
            return ;
        }
        ExamReservation examReservation = new ExamReservation(teacher_id, scheduling_name,
                2, course_name, date, time_num);
        Long examReservationId = reservationMapper.getLastReservationId() + 1;
        examReservation.setId(examReservationId);
        reservationMapper.addExamReservation(examReservation);
        addReservationItem(examReservation.getId(),examReservation.getName(),selectedComputerRoomList,klassList,null,false,reservation_type);

    }

    //19 生成reservationItem
    @Override
    public void addReservationItem(Long reservation_id, String reservation_name,List<ComputerRoom> computerRoomList, List<Klass> klassList,List<String>student_id_list, boolean is_shared,Integer type) {
        System.out.println(type);
        Integer student_num = 0;
        List<User> userList = userMapper.getUserListByClassList(klassList);
        System.out.println(userList);
        if(type == 4){
            userList = userMapper.getUserListByIdList(student_id_list);
        }
        Iterator<User> it = userList.iterator();
        for(Klass klass:klassList ){
            student_num += klass.getStudent_num();
        }
        if(is_shared){
            for(ComputerRoom computerRoom : computerRoomList){
                Integer studentInThisRoom;
                if(student_num>computerRoom.getRemain_num()){
                    studentInThisRoom = computerRoom.getRemain_num();
                }
                else{
                    studentInThisRoom = student_num;
                }
                student_num -= studentInThisRoom;
                for(Klass klass :klassList){
                    ReservationItem reservationItem = new ReservationItem(reservation_id, computerRoom.getId(),reservation_name,klass.getId(), studentInThisRoom ,1,type);
                    Long reservationItemId = reservationMapper.getLastReservationItemId() + 1;
                    reservationItem.setId(reservationItemId);
                    reservationMapper.addReservationItem(reservationItem);
                }
                if(student_num == 0){
                    break;
                }
            }
        }
        else{
            int std_student_num = student_num / computerRoomList.size();
            int remain_num = student_num % computerRoomList.size();
            for(ComputerRoom computerRoom : computerRoomList) {
                Integer studentInThisRoom;
                if (remain_num != 0) {
                    studentInThisRoom = std_student_num + 1;
                    remain_num--;
                } else {
                    studentInThisRoom = std_student_num;
                }
                System.out.println("==============================");
                System.out.println(studentInThisRoom);
                System.out.println(computerRoom);
                System.out.println("+++++++++++++++++++++++++++++++++");
                if(type == 2 ||type == 4) {
                    ReservationItem reservationItem = new ReservationItem(reservation_id, computerRoom.getId(),reservation_name,"0", studentInThisRoom ,0,type);
                    Long reservationItemId = reservationMapper.getLastReservationItemId() + 1;
                    reservationItem.setId(reservationItemId);
                    reservationMapper.addReservationItem(reservationItem);
                    Long Min = 1000000000L;
                    while(it.hasNext()){
                        User user = it.next();
                        Record record  = new Record(user.getId(),reservationItem.getId(),reservation_name,type,1);
                        Long recordId = recordMapper.getLastRecordId() + 1;
                        record.setId(recordId);
                        Min = Math.min(Min,record.getId());
                        record.setSeat_num(Integer.parseInt(String.valueOf(record.getId()-Min+1L)));
                        studentInThisRoom--;
                        recordMapper.addRecord(record);
                        System.out.println(record);
                        if(studentInThisRoom == 0)
                            break;
                    }
                }
                else{
                    for(Klass klass :klassList){
                        ReservationItem reservationItem = new ReservationItem(reservation_id, computerRoom.getId(),reservation_name,klass.getId(), studentInThisRoom ,0,type);
                        Long reservationItemId = reservationMapper.getLastReservationItemId() + 1;
                        reservationItem.setId(reservationItemId);
                        reservationMapper.addReservationItem(reservationItem);
                        if(student_num == 0)
                            break;
                    }
                }
            }
        }
    }

    //20 通过实验名称、老师名字、日期查询课程

    @Override
    public List<Reservation> getReservationList(String scheduling_name, String teacher_name, String date) {
        List<String> teacherIdList = userMapper.getUserIdListLikeNameAndType(teacher_name,2);
        System.out.println(teacherIdList);
        if (teacherIdList.isEmpty())
            return null;
        List<KlassReservation> klassReservationList = reservationMapper.getKlassReservationListByInformation(scheduling_name, teacherIdList, date);
        return mergeReservation(klassReservationList,null,null,null);
    }

    //√
    //21 将课程列表和考试列表合并
    @Override
    public List<Reservation> mergeReservation(List<KlassReservation> klassReservations, List<ExamReservation> examReservations ,
                                              List<LabReservation> labReservations,List<CompetitionReservation> competitionReservations) {
        List<Reservation> reservationList = new ArrayList<>();
        if(klassReservations != null){
            for(KlassReservation klassReservation :  klassReservations){
                reservationList.add(klassReservation.toReservation());
            }
        }
        if(examReservations != null){
            for(ExamReservation examReservation : examReservations){
                reservationList.add(examReservation.toReservation());
            }
        }
        if(labReservations != null){
            for(LabReservation labReservation : labReservations){
                reservationList.add(labReservation.toReservation());
            }
        }if(competitionReservations != null){
            for(CompetitionReservation competitionReservation : competitionReservations){
                reservationList.add(competitionReservation.toReservation());
            }
        }
        return reservationList;
    }

    //22 显示所有机房安排
    @Override
    public List<ReservationItem> getAllRoomReservation() {
        //ToDo
        return reservationMapper.getAllReservationItem();
    }

    //23 显示所有课程实验安排
    @Override
    public List<KlassReservation> getAllKlassReservation() {
        return reservationMapper.getAllKlassReservation();
    }

    //24 显示所有考试安排
    @Override
    public List<ExamReservation> getAllExamReservation() {
        return reservationMapper.getAllExamReservation();
    }

    //25 显示所有比赛安排
    @Override
    public List<CompetitionReservation> getAllCompetitionReservation() {
        return reservationMapper.showAllCompetitionReservationList();
    }

    //√
    //26 通过日期、序号获得所有预约列表
    @Override
    public List<Reservation> getReservationListByDateAndTimeNum(Date date, Integer time_num) {
        List<KlassReservation> klassReservationList = reservationMapper.getKlassReservationListByTimeAndNum(date, time_num);
        List<ExamReservation> examReservationList = reservationMapper.getExamReservationListByTimeAndNum(date, time_num);
        return mergeReservation(klassReservationList,examReservationList,null,null);
    }

    //√
    //27 显示当前开放性实验
    @Override
    public List<LabReservation> getNowLabReservationList() {
        Date date = new Date(System.currentTimeMillis());
        return reservationMapper.getLabReservationListByDate(date);
    }

    //√
    //28 显示所有开放性实验
    @Override
    public List<ReservationItem> getAllLabReservationList() {
        List<LabReservation> labReservations = reservationMapper.showAllLabReservationList();
        List<Long> labReservationIdList = new ArrayList<>();
        for(LabReservation labReservation:labReservations){
            labReservationIdList.add(labReservation.getId());
        }
        return reservationMapper.getReservationItemList(mergeReservation(null,null,labReservations,null));
    }

    //29
    @Override
    public List<ReservationItem> getSelectedLabReservation(String student_id) {
        List<Record> recordList = recordMapper.getRecordListByStudentIDAndType(student_id,3);
        List<ReservationItem> reservationItemList = new ArrayList<>();
        for(Record record :recordList){
            ReservationItem reservationItem = reservationMapper.getReservationItemByItemId(record.getReservationItem_id());
            reservationItemList.add(reservationItem);
        }
        return reservationItemList;
    }

    //30 选择开放性实验加入
    @Override
    public Integer selectLabReservation(Long reservation_id,String student_id) {
        LabReservation labReservation = reservationMapper.getLabReservationListById(reservation_id);
        if(labReservation.select()==0){
            return 0;
        }
        Record record = new Record(student_id,reservation_id,labReservation.getName(),3,1);
        recordMapper.addRecord(record);
        reservationMapper.updateLabReservation(labReservation);
        return 1;
    }

    //31 选择开放性实验退出
    @Override
    public Integer quitLabReservation(Long reservation_id, String student_id) {
        LabReservation labReservation = reservationMapper.getLabReservationListById(reservation_id);
        if(labReservation.quit()==0){

            return 0;
        }
        Record record = recordMapper.getRecordByItemId(reservation_id);
        record.cancel();
        recordMapper.quitRecord(record);
        reservationMapper.updateLabReservation(labReservation);
        return 1;
    }

    //√
    //32 初始化机房
    @Override
    public List<ComputerRoom> initComputerRoomList(List<ComputerRoom> computerRoomList) {
        for (ComputerRoom computerRoom : computerRoomList)
            computerRoom.setRemain_num(computerRoom.getNum());
        return computerRoomList;
    }

    @Override
    public List<CourseInfo> getCourseInfoList(List<ReservationItem> reservationItemList) {
        List<CourseInfo> courseInfos = new ArrayList<>();
        for(ReservationItem reservationItem :reservationItemList){
            courseInfos.add(getCourseInfo(reservationItem));
        }
        return courseInfos;
    }

    @Override
    public CourseInfo getCourseInfo(ReservationItem reservationItem) {
        CourseInfo courseInfo = new CourseInfo();
        if(reservationItem.getType() == 1){
            KlassReservation klassReservation = reservationMapper.getKlassReservationById(reservationItem.getReservation_id());
            String name = userMapper.getUserById(klassReservation.getUser_id()).getName();
             courseInfo = new CourseInfo(reservationItem.getId(),reservationItem.getReservation_id(),1,name,
                    klassReservation.getCourse_name(),klassReservation.getName(),
                    klassReservation.getDate(),klassReservation.getTime_num(),
                    reservationItem.getComputer_room_id());
        }
        if(reservationItem.getType() == 2){
            ExamReservation examReservation = reservationMapper.getExamReservationById(reservationItem.getReservation_id());
            String name = userMapper.getUserById(examReservation.getUser_id()).getName();
             courseInfo = new CourseInfo(reservationItem.getId(),reservationItem.getReservation_id(),2,
                    examReservation.getCourse_name(),examReservation.getName(),
                    examReservation.getDate(),examReservation.getTime_num(),
                    reservationItem.getComputer_room_id(),Integer.parseInt(reservationItem.getKlass_id()));
        }
        if(reservationItem.getType() == 3){
            LabReservation labReservation = reservationMapper.getLabReservationById(reservationItem.getReservation_id());
            String name = userMapper.getUserById(labReservation.getUser_id()).getName();
             courseInfo = new CourseInfo(reservationItem.getId(),reservationItem.getReservation_id(),3,name,
                    labReservation.getBegin_date(),labReservation.getEnd_date(),
                    labReservation.getPlan_num(),labReservation.getRemain_num());
        }
        if(reservationItem.getType() == 4){
            CompetitionReservation competitionReservation = reservationMapper.getCompetitionReservationById(reservationItem.getReservation_id());
            String name = userMapper.getUserById(competitionReservation.getUser_id()).getName();
             courseInfo = new CourseInfo(reservationItem.getId(),reservationItem.getReservation_id(),4,competitionReservation.getName(),
                    competitionReservation.getDate(),reservationItem.getComputer_room_id(),
                    competitionReservation.getBegin_time(),competitionReservation.getEnd_time(),Integer.parseInt(reservationItem.getKlass_id()));
        }
        return courseInfo;
    }


}
